package com.timetask;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.bpms.common.CommonUtil;
import com.bpms.common.FileOperation;
import com.bpms.common.GZipUtils;
import com.bpms.common.Param;
import com.zk.atm.utils.AccessDBConnection;

/**
 * 更新accountnum表定时任务
 * 
 * @author menlin
 * 
 */
public class UpdateAccountTimetask {

	private int batch = 1000;
	private Connection conn = null;
	private String path = null;

	public void execute() {
		path = Param.getValue("ATMP", "atmp_iccard_datasavepath");
		if (gunzipData()) {
			try {
				// 清空表accountnum
				clearTable();
				// 插入数据
				insert();
			} catch (Exception e) {
				CommonUtil.error("更新accountnum表定时任务发生异常：", e);
			}
		}
	}

	/**
	 * 解压Linux下的.gz包
	 * 
	 * @throws Exception
	 */
	private boolean gunzipData() {
		String gzpath = path + ".gz";
		File file = new File(gzpath);
		if (file.exists() && file.isFile()) {
			try {
				GZipUtils.decompress(file);
			} catch (Exception e) {
				CommonUtil.error("解压card_data.gz异常：", e);
				return true;
			}
		} 
		CommonUtil.error("UpdateAccountTimetask：card_data.gz文件不存在");
		return true;
	}

	/**
	 * 清空数据
	 * 
	 * @throws Exception
	 */
	private void clearTable() throws Exception {
		this.getConn();
		String sql = "DELETE accountnum";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.execute();
		conn.commit();
		pstmt.close();
		this.closeConn();
	}

	/**
	 * 批处理插入数据
	 * 
	 * @throws Exception
	 */
	private void insert() throws Exception {
		this.getConn();
		String sql = "INSERT INTO accountnum(id,cardid,account,usrname) VALUES (?,?,?,?)";
		List<String> list = getData();
		conn.setAutoCommit(false);
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.clearBatch();
		int size = list.size();
		for (int i = 0; i < size; i++) {
			String element = list.get(i);
			if (element != null) {
				element = element.replace("|", ",");
				String[] arr = element.split(",");
				ps.setString(1, UUID.randomUUID().toString().replace("-", ""));
				ps.setString(2, arr[0].trim());
				ps.setString(3, arr[1].trim());
				if (arr.length == 3) {
					ps.setString(4, arr[2].trim());
				} else {
					ps.setString(4, "");
				}
				ps.addBatch();
				if (i > 0 && i % batch == 0)
					ps.executeBatch();
			}
		}
		list.clear();
		list = null;
		ps.executeBatch();
		ps.close();
		ps = null;
		this.closeConn();
	}
	
	/**
	 * 获取需要持久化的数据<br>包括测试数据和解压后真实数据
	 * @return List
	 */
	private List<String> getData(){
		List<String> result = new ArrayList<String>();
		// 应行方要求加入测试数据
		File testDataFile = new File(path + "_test");
		if (testDataFile.exists() && testDataFile.isFile()) {
			// 测试数据包存在
			FileOperation fileOperation2 = new FileOperation(testDataFile);
			List<String> tlist = fileOperation2
			.getContentByLine(FileOperation.GBK);
			if (tlist != null && !tlist.isEmpty())
				result.addAll(tlist);
		}
		// 获取解压后数据包
		File dataFile = new File(path);
		if (dataFile.exists()) {
			FileOperation fileOperation = new FileOperation(dataFile);
			List<String> list = fileOperation.getContentByLine(FileOperation.GBK);
			if (list != null && !list.isEmpty())
				result.addAll(list);
		} else {
			CommonUtil.error("UpdateAccountTimetask ");
		}
		return result;
	}

	/**
	 * 获取数据库连接
	 * 
	 * @return
	 * @throws Exception
	 */
	private void getConn() throws Exception {
		if (conn == null || conn.isClosed()) {
			conn = AccessDBConnection.getConnection();
		}
	}

	/**
	 * 关闭连接
	 * 
	 * @throws Exception
	 */
	private void closeConn() {
		if (conn != null) {
			try {
				if (!conn.isClosed())
					conn.close();
			} catch (SQLException e) {
				CommonUtil.error("更新accountnum表定时任务关闭数据库连接发生异常：", e);
			}
			conn = null;
		}
	}

	public static void main(String[] args) {
		File f = new File("e:/card_data_test");
		if (f.exists() && f.isFile()) {
			FileOperation fileOperation = new FileOperation(f);
			List<String> list = fileOperation
					.getContentByLine(FileOperation.GBK);
			if (list != null && !list.isEmpty()) {
				for (int i = 0; i < list.size(); i++) {
					System.out.println(list.get(i));
				}
			}
		}
	}
}
